package com.g4m.baselib.entity;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created on 2020-01-01
 * <p>Copyright 2008-2020 snsndk.com</p>
 */
@Slf4j
public class PublicDataHolder {
    private static final HystrixRequestVariableDefault<HttpServletRequest> HYSTRIX_REQUEST_CONTEXT = new HystrixRequestVariableDefault<>();

    private static final InheritableThreadLocal<SnsNdkPublic> INHERITABLE_THREAD_LOCAL_PUBLIC = new InheritableThreadLocal<>();

    /**
     * 获取请求公共P信息
     *
     * @return SnsNdkPublic
     */
    public static SnsNdkPublic getSnsNdkPublic() {
        SnsNdkPublic snsNdkPublicObj = INHERITABLE_THREAD_LOCAL_PUBLIC.get();
        if (snsNdkPublicObj != null) {
            return snsNdkPublicObj;
        }
        return null;
    }

    /**
     * 获取请求公共P信息
     *
     * @param httpRequest HttpServletRequest
     * @return SnsNdkPublic
     */
    public static SnsNdkPublic getSnsNdkPublic(HttpServletRequest httpRequest) {
        Object snsNdkPublicObj = httpRequest.getAttribute("snsNdkPublic");
        if (snsNdkPublicObj instanceof SnsNdkPublic) {
            return (SnsNdkPublic) snsNdkPublicObj;
        }
        return null;
    }

    /**
     * 获取请求公共P信息
     *
     * @param httpRequest  HttpServletRequest
     * @param snsNdkPublic SnsNdkPublic
     */
    public static void setSnsNdkPublic(HttpServletRequest httpRequest, SnsNdkPublic snsNdkPublic) {
        httpRequest.setAttribute("snsNdkPublic", snsNdkPublic);
        INHERITABLE_THREAD_LOCAL_PUBLIC.set(snsNdkPublic);
    }

    /**
     * s参数中获取数据值
     *
     * @param httpRequest HttpServletRequest
     * @param key         key值
     * @return value值
     */
    @SuppressWarnings("unchecked")
    public static String getStringFromS(HttpServletRequest httpRequest, String key) {
        Object snsNdkSecretObj = httpRequest.getAttribute("snsNdkSecret");
        if (snsNdkSecretObj instanceof Map) {
            Map<String, String> snsNdkSecret = (Map<String, String>) snsNdkSecretObj;
            return snsNdkSecret.get(key);
        }
        return null;
    }

    /**
     * 获取请求公共P信息
     *
     * @param httpRequest  HttpServletRequest
     * @param snsNdkSecret snsNdkSecret
     */
    public static void setSnsNdkSecret(HttpServletRequest httpRequest, Map<String, String> snsNdkSecret) {
        httpRequest.setAttribute("snsNdkSecret", snsNdkSecret);
    }

    /**
     * 执行完后清除现场
     *
     * @param httpRequest 请求
     */
    public static void clear(HttpServletRequest httpRequest) {
        INHERITABLE_THREAD_LOCAL_PUBLIC.remove();
        if (httpRequest != null) {
            httpRequest.removeAttribute("snsNdkHeader");
            httpRequest.removeAttribute("snsNdkPublic");
            httpRequest.removeAttribute("snsNdkSecret");
        }

        if (HystrixRequestContext.isCurrentThreadInitialized()) {
            HystrixRequestContext.getContextForCurrentThread().shutdown();
        }
    }

    /**
     * 获取httpRequest上下文
     *
     * @return 请求
     */
    public static HttpServletRequest getHttpServletRequest() {
        initHystrixRequestContext();
        return HYSTRIX_REQUEST_CONTEXT.get();
    }

    /**
     * 设置httpRequest上下文
     *
     * @param httpRequest 请求
     */
    public static void setHttpServletRequest(HttpServletRequest httpRequest) {
        initHystrixRequestContext();
        HYSTRIX_REQUEST_CONTEXT.set(httpRequest);
    }

    /**
     * 初始化
     */
    private static void initHystrixRequestContext() {
        if (!HystrixRequestContext.isCurrentThreadInitialized()) {
            HystrixRequestContext.initializeContext();
        }
    }
}
